package components {
	import common.Icon;
	
	import mx.containers.HBox;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.events.FlexEvent;

	public class MessageItem extends HBox {
		
		//------------------------------------------------------------
        //
        // class constants
        //
        //------------------------------------------------------------
		
		/**
		 * used for reqeust failed
		 */
		public static const RESULT_ERROR:String = "resulterror";
		
		/**
		 * used for response data error
		 */
		public static const RESULT_WARNING:String = "resultwarning";
		
		/**
		 * used for request finished normally
		 */
		public static const RESULT_OK:String = "resultok";
		
		/**
		 * used for panel display initial info
		 */
		public static const INIT:String = "initialize";
		
		//------------------------------------------------------------
        //
        // variables
        //
        //------------------------------------------------------------
		
		private var _mode:String;
		private var _message:String;
		
		//------------------------------------------------------------
        //
        // constructor
        //
        //------------------------------------------------------------
		
		public function MessageItem(mode:String, message:String = null) {
			super();
			
			_mode = mode;
			_message = message;
			
			percentWidth = 100;
			percentHeight = 100;
			
			addEventListener(FlexEvent.INITIALIZE, onInit);
		}
		
		//------------------------------------------------------------
        //
        // event handlers
        //
        //------------------------------------------------------------

		private function onInit(e:FlexEvent):void {
			var img:Image;
			var text:Text;
			
			switch (_mode) {
				case RESULT_ERROR: {
					img = new Image();
					img.source = Icon.ERROR;
					
					text = new Text();
					text.text = "很抱歉，服务器出错。请再试一次。";
					
					break;
				}
				
				case RESULT_WARNING: {
					img = new Image();
					img.source = Icon.WARNING;
					
					text = new Text();
					text.text = "出错了，操作失败。请重新操作。";
					
					break;
				}
				
				case RESULT_OK: {
					img = new Image();
					img.source = Icon.OK;
					
					text = new Text();
					text.text = "恭喜，操作成功！";
					
					break;
				}
				
				case INIT: {
					text = new Text();
					text.text = "详细信息面板";
					text.setStyle("fontSize", 20);
					text.setStyle("color", 0x999999);
					
					break;
				}
			}
			
			if (img) {
				addChild(img);
			}
			
			if (text) {
				if (img) {
					text.setStyle("paddingTop", 5);
				}
				if (_message) {
					text.text = _message;
				}
				text.maxWidth = 330;
				addChild(text);
			}
		}
		
	}
}